﻿Imports EstoqueDAL
Imports EstoqueBLL
Class FabricanteCAD
    Dim objBLL As New FabricanteBLL
    Dim operacao As Char
#Region "Eventos"
#Region "Form Closing"
    Private Sub FabricanteCAD_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        limpar()
    End Sub
#End Region

#Region "KeyDown do Form"
    Private Sub FabricanteCAD_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        If (e.KeyCode = Keys.Enter) Then
            SendKeys.Send("{TAB}")
        End If
        If (e.KeyCode = Keys.Escape) Then
            Me.Close()
        End If
    End Sub
#End Region

#Region "Load"
    Private Sub FabricanteCAD_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'sta_codigo: 1- ativo, 2 inativo
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo=1 or sta_codigo=2", "sta_descricao")
        If (Not IsNothing(lcodigo.Text) And lcodigo.Text.Trim <> "") Then
            objBLL = New FabricanteBLL(Val(lcodigo.Text))
            carregacampos()
            operacao = "A"
            lcodigo.Visible = True
        Else : operacao = "I"
        End If
    End Sub
#End Region

#Region "Salvar"
    Private Sub TSBGravar_Click(sender As System.Object, e As System.EventArgs) Handles TSBGravar.Click
        Try
            'carregar campos
            objBLL = New FabricanteBLL(Val(lcodigo.Text), StrConv(LTrim(RTrim(TDescricao.Text)), VbStrConv.ProperCase), CBStatus.SelectedValue)
            If operacao = "I" Then
                If (objBLL.inserir(objusuarios.Codigo)) Then
                    MessageBox.Show("Fabricante inserido com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("O fabricante não pôde ser inserido", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            Else
                If (objBLL.alterar(objusuarios.Codigo)) Then
                    MessageBox.Show("Fabricante alterado com sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    Me.Close()
                Else : MessageBox.Show("O fabricante não pôde ser alterado", "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try

    End Sub
#End Region

#Region "Voltar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region
#End Region

#Region "Métodos"
#Region "Limpar"
    Public Sub limpar()
        Me.TDescricao.Clear()
        Me.CBStatus.SelectedValue = 0
        Me.objBLL = Nothing
        Me.lcodigo.Text = Nothing
    End Sub
#End Region

#Region "Carregar Campos"
    Public Sub carregacampos()
        TDescricao.Text = objBLL.fab_descricao
        CBStatus.SelectedValue = objBLL.sta_codigo
        lcodigo.Text = objBLL.fab_codigo
    End Sub
#End Region
#End Region

End Class